﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : IRecruitment
    {
        public int Recruitment_Using(int RecruitmentID)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Recruitment_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@RecruitmentID", RecruitmentID);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Recruitment_GetAll(int Year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Recruitment_GetAll", dbConn);
            dbCmd.Parameters.AddWithValue("@Year", Year);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Recruitment_GetList(out int ItemCount, string keyWord, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Recruitment_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@KeyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public RecruitmentInfo Recruitment_GetInfo(int _Recruitment_ID)
        {
            RecruitmentInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Recruitment_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Recruitment_ID", _Recruitment_ID);
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new RecruitmentInfo();
                    retVal.ID = Convert.ToInt32(dr["Recruitment_ID"]);
                    retVal.Name = Convert.ToString(dr["Recruitment_Name"]);
                    retVal.FromDate = dr["Recruitment_FromDate"] == DBNull.Value? DateTime.MaxValue: Convert.ToDateTime(dr["Recruitment_FromDate"]);
                    retVal.ToDate = dr["Recruitment_ToDate"] == DBNull.Value ? DateTime.MaxValue : Convert.ToDateTime(dr["Recruitment_ToDate"]);
                    retVal.Description = Convert.ToString(dr["Recruitment_Description"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Recruitment_Insert(RecruitmentInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Recruitment_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            dbCmd.Parameters.AddWithValue("@Recruitment_Name", eInfo.Name);
            if (eInfo.FromDate != DateTime.MinValue)
            dbCmd.Parameters.AddWithValue("@Recruitment_FromDate", eInfo.FromDate);
            else
            dbCmd.Parameters.AddWithValue("@Recruitment_FromDate", DBNull.Value);
            if (eInfo.ToDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Recruitment_ToDate", eInfo.ToDate);
            else
                dbCmd.Parameters.AddWithValue("@Recruitment_ToDate", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@Recruitment_Description", eInfo.Description);


            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Recruitment_Update(RecruitmentInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Recruitment_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Recruitment_ID", eInfo.ID);

            dbCmd.Parameters.AddWithValue("@Recruitment_Name", eInfo.Name);
            if (eInfo.FromDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Recruitment_FromDate", eInfo.FromDate);
            else
                dbCmd.Parameters.AddWithValue("@Recruitment_FromDate", DBNull.Value);
            if (eInfo.ToDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Recruitment_ToDate", eInfo.ToDate);
            else
                dbCmd.Parameters.AddWithValue("@Recruitment_ToDate", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@Recruitment_Description", eInfo.Description); try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Recruitment_Delete(int RecruitmentID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Recruitment_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Recruitment_ID", RecruitmentID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Recruitment_Exist(int _ID, string _Name)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Recruitment_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Recruitment_ID", _ID);
            dbCmd.Parameters.AddWithValue("@Recruitment_Name", _Name);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
